const { writeFile,mkdir,existsSync } = require('fs');
const util = require('util');

const write = util.promisify(writeFile);
const mk = util.promisify(mkdir);

const saveFile = async ({ rootPath,file }) => {
	const done = async () => {
		await write(`${rootPath}/${file.originalname}`,file.buffer);
		await write(`${rootPath}/${file.originalname}.back`,file.buffer);
	};
	if(existsSync(rootPath)){
		await done();
	}else{
		await mk(rootPath);
		await done();
	}
};

module.exports = { saveFile };
